const jwt = require('jsonwebtoken');

const { tokenExpiredTime } = require('./config')

//秘钥
const {secretKey : signkey} = require('./config')
//生成token
const setToken = function (username,id) {
    return new Promise((resolve, reject) => {
        const token = jwt.sign({
            username: username,
            id : id
        }, signkey, { expiresIn:  tokenExpiredTime });
        resolve(token);
    })
}
//验证token
const verToken = function (token) {
    return new Promise((resolve, reject) => {
        var info = jwt.verify(token, signkey ,(error, decoded) => {
            if (error) {
                console.log('token过期')
                resolve("timeout")
            }
            resolve(decoded)
        });

    })
}

module.exports = {
    setToken,
    verToken
}